package com.aosan.website.admin.service.impl

import com.aosan.website.admin.dto.User
import com.aosan.website.admin.entity.SysUserEntity
import com.aosan.website.admin.mapper.SysUserMapper
import com.aosan.website.admin.service.RoleService
import com.aosan.website.admin.service.UserService
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class SysUserServiceImpl(
        val roleService: RoleService
) : UserService, ServiceImpl<SysUserMapper, SysUserEntity>() {
    val logger = LoggerFactory.getLogger(this::class.java)

    /**
     * getUserByUserName
     * 根据用户名获取用户信息
     *
     * @param userName
     * @return SysUserEntity
     */
    override fun getUserByUserName(userName: String): SysUserEntity? {
        return this.list(QueryWrapper<SysUserEntity>()
                .eq("account", userName))?.let { list ->
            if (list.size == 0)
                return null
            list.elementAt(0).apply {
                roleId = roleId?.let { roleService.getRolesNameByRoleID(it) }
            }
        }
    }

    /**
     * getUserList
     * 获取所有用户基本信息
     *
     * @return list
     */
    override fun getUserList(): Any {
        val list = mutableListOf<User>()
        this.list(null).forEach { userEntity ->
            list.add(
                    User(
                            userEntity.account,
                            userEntity.name,
                            userEntity.sex!!,
                            userEntity.avatar!!,
                            userEntity.email!!,
                            userEntity.birthday!!,
                            userEntity.roleId!!,
                            userEntity.deptId!!
                    )
            )
        }
        return list
    }
}